package com.fzu.ess.sys.entity;

import com.fzu.ess.common.entity.BaseEntity;
import lombok.*;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ercha on 2015/10/16.
 */
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
//@Getter
//@Setter
@DynamicInsert @DynamicUpdate
@Table(name = "sys_user")
public class User extends BaseEntity {

    @Column(name = "username")
    private String username;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;

    @Column(name = "salt")
    private String salt;

    /**
     0：超级管理员
     1：管理员
     2：普通用户
     3: 未激活用户
     4: 冻结用户
     5: 封号用户
     */
    @Column(name = "level")
    private Integer level;          //账户等级


    /*@Column(name = "status")
    private byte status;            //1：未激活，2：正常，3：冻结*/

    @ManyToMany
    @JoinTable(name = "sys_user_role",
            joinColumns = { @JoinColumn(name = "user_id") },
            inverseJoinColumns = { @JoinColumn(name = "role_id") })
    @OrderBy("id") @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<Role> roleList = new ArrayList<>(); // 拥有角色列表

//    @Transient
//    public List<Integer> getRoleIdList() {
//        List<Integer> roleIdList = new ArrayList<Integer>();
//        for (Role role : roleList) {
//            roleIdList.add(role.getId());
//        }
//        return roleIdList;
//    }
//
//    @Transient
//    public void setRoleIdList(List<Integer> roleIdList) {
//        roleList = new ArrayList<>();
//        for (Integer roleId : roleIdList) {
//            Role role = new Role();
//            role.setId(roleId);
//            roleList.add(role);
//        }
//    }
//
//    /**
//     * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
//     */
//    @Transient
//    public String getRoleNames() {
//        return Collections3.extractToString(roleList, "name", ", ");
//    }
}
